package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallKeyword;
import org.linlinjava.litemall.db.mapper.LitemallKeywordDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallKeywordService {
    @Resource
    private LitemallKeywordDao keywordsMapper;

    public LitemallKeyword queryDefault() {
    	
    	LitemallKeyword query = new LitemallKeyword();
    	query.setIsDefault(true);
    	query.setDeleted(false);
    	return keywordsMapper.templateOne(query);
    	
		/*
		 * LitemallKeywordExample example = new LitemallKeywordExample();
		 * example.or().andIsDefaultEqualTo(true).andDeletedEqualTo(false); return
		 * keywordsMapper.selectOneByExample(example);
		 */
    }

    public List<LitemallKeyword> queryHots() {
    	
    	LitemallKeyword query = new LitemallKeyword();
    	query.setIsHot(true);
    	query.setDeleted(false);
    	return keywordsMapper.template(query);
    	
		/*
		 * LitemallKeywordExample example = new LitemallKeywordExample();
		 * example.or().andIsHotEqualTo(true).andDeletedEqualTo(false); return
		 * keywordsMapper.selectByExample(example);
		 */
    }

    public List<LitemallKeyword> queryByKeyword(String keyword, Integer page, Integer limit) {
    	
    	LambdaQuery<LitemallKeyword> query = keywordsMapper.createLambdaQuery();
    	
    	query.distinct();
    	query.andLike(LitemallKeyword::getKeyword, "%" + keyword + "%");
    	query.andEq(LitemallKeyword::getDeleted, false);
    	query.limit(page, limit);
    	return query.select(LitemallKeyword::getKeyword);
    	
		/*
		 * LitemallKeywordExample example = new LitemallKeywordExample();
		 * example.setDistinct(true); example.or().andKeywordLike("%" + keyword +
		 * "%").andDeletedEqualTo(false); PageHelper.startPage(page, limit); return
		 * keywordsMapper.selectByExampleSelective(example,
		 * LitemallKeyword.Column.keyword);
		 */
    }

    public List<LitemallKeyword> querySelective(String keyword, String url, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallKeyword> query = keywordsMapper.createLambdaQuery();
    	
    	
        if (!StringUtils.isEmpty(keyword)) {
        	query.andLike(LitemallKeyword::getKeyword, "%" + keyword + "%");
            //criteria.andKeywordLike("%" + keyword + "%");
        }
        if (!StringUtils.isEmpty(url)) {
        	query.andLike(LitemallKeyword::getUrl, "%" + keyword + "%");
            //criteria.andUrlLike("%" + url + "%");
        }
        query.andEq(LitemallKeyword::getDeleted, false);
        //criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        query.limit(page, limit);
        return query.select();
		/*
		 * PageHelper.startPage(page, limit); return
		 * keywordsMapper.selectByExample(example);
		 */
    }

    public void add(LitemallKeyword keywords) {
        keywords.setAddTime(LocalDateTime.now());
        keywords.setUpdateTime(LocalDateTime.now());
        keywordsMapper.insertTemplate(keywords,true);
        //keywordsMapper.insertSelective(keywords);
    }

    public LitemallKeyword findById(Integer id) {
    	return keywordsMapper.single(id);
        //return keywordsMapper.selectByPrimaryKey(id);
    }

    public int updateById(LitemallKeyword keywords) {
        keywords.setUpdateTime(LocalDateTime.now());
        return keywordsMapper.updateTemplateById(keywords);
        //return keywordsMapper.updateByPrimaryKeySelective(keywords);
    }

    public void deleteById(Integer id) {
    	LitemallKeyword keyword = new LitemallKeyword();
    	keyword.setDeleted(true);
    	keyword.setId(id);
    	keywordsMapper.updateTemplateById(keyword);
        //keywordsMapper.logicalDeleteByPrimaryKey(id);
    }
}
